jQuery(document).ready(function () {
	hideOrderDetail();
	hideOrder();
	hidePreviewOrder();
	hideFormDate();
	
	//Event click order
	jQuery("#list-order tbody td").click(function () {
		hidePreviewOrder();
		hideFormDate();
		if (jQuery(this).hasClass("selected")) {
			jQuery(this).removeClass("selected");
			hideOrderDetail();
		} else {
			showOrderDetail();
			hideOrder();
			jQuery(this).addClass("selected");
			
			var stt = jQuery(this).index();
			
			jQuery("#list-order-detail tbody tr:eq("+stt+")").addClass("selected").fadeIn(500);
			jQuery(".order-detail-button .buttonlist").fadeIn(500);
			
			
		}
	});
	
	//Minus number 
	jQuery("#list-order-detail .change-number .minus").click(function () {
		var number = parseInt(jQuery(this).parent().find(".number").html());
		var max = parseInt(jQuery(this).parent().find(".max").html());
		
		var result = number - 1; 
		if (result <= 0) {
			result = 0;
		}
		jQuery(this).parent().find(".number").html(result);
		jQuery(this).parent().find(".store-return").html(max-result);
		
		//Update preview order
		var index = jQuery(this).parents(".change-number").index();
		var stt = jQuery(this).parents("tr").index();
		jQuery("#form-preview-order tbody tr:eq("+stt+") .change-number:eq("+index+") .number").html(result);
	});
	
	//Plus number 
	jQuery("#list-order-detail .change-number .plus").click(function () {
		var number = parseInt(jQuery(this).parent().find(".number").html());
		var max = parseInt(jQuery(this).parent().find(".max").html());
		
		var result = number + 1; 
		if (result >= max) {
			result = max;
		}
		
		jQuery(this).parent().find(".number").html(result);
		jQuery(this).parent().find(".store-return").html(max-result);
		
		//Update preview order
		var index = jQuery(this).parents(".change-number").index();
		var stt = jQuery(this).parents("tr").index();
		jQuery("#form-preview-order tbody tr:eq("+stt+") .change-number:eq("+index+") .number").html(result);
	});
	
	//Event click status
	jQuery(".buttonlist button").click(function () {
		var status = jQuery(this).val();
		
		jQuery("#list-order-detail tr.selected .change-number .status").html(status);
		//Remove all class status-*
		jQuery("#list-order tbody td.selected").removeClass(function(index, cls) {
            var re = "status-*".replace(/\*/g, '\\S+');
            return (cls.match(new RegExp('\\b' + re + '', 'g')) || []).join(' ');
        });
		
		if (status == 3) {
			if (jQuery(this).parent().hasClass("show")) {
				var date = jQuery(this).parent().find(".date-return").val();
				if (date == "") {
					alert("Bạn chưa nhập ngày hẹn lại");
					return false;
				}
				cancelValue();
				hideFormDate();
				jQuery("#list-order-detail tr.selected .change-number .date").html(date);
				jQuery("#list-order tbody td.selected").addClass("checked status-" + status);
				//Remove class selected
				jQuery("#list-order tbody td.selected").removeClass("selected");
				hideOrderDetail();
			} else {
				showFormDate();
			}
		} else {
			if (status > 1) {
				cancelValue();
			}
			
			jQuery("#list-order tbody td.selected").addClass("checked status-" + status);
			//Remove class selected
			jQuery("#list-order tbody td.selected").removeClass("selected");
			hideOrderDetail();
		}
		
		return false;
	});
	
	jQuery(".order-accept").click(function () {
		hideOrder();
		var count = jQuery("#list-order tbody td").size();
		var count_check = jQuery("#list-order tbody td.checked").size();
		
		if (count != count_check) {
			alert("Bạn chưa kiểm tra hết đơn hàng.");
			hidePreviewOrder();
			return false;
		} else {
			var listOrder = getAllValue();
			jQuery("#form-preview-order #listOrder").val(listOrder);
			calcMoney();
			hideOrderDetail();
			showPreviewOrder();
		}
		return false;
	});
	
	jQuery(".buttonlist .form-date .date-return").datepicker({
		dateFormat: "dd-mm-yy"
	});
	
	function hideOrder() {
		jQuery("#list-order-detail tbody tr").hide();
		jQuery("#list-order tbody td").removeClass("selected");
		jQuery(".order-detail-button .buttonlist").hide();
		jQuery("#list-order-detail tbody tr").removeClass("selected");
	}
	
	function hideOrderDetail() {
		jQuery("#form-detail-order").hide();
	}
	
	function showOrderDetail() {
		jQuery("#form-detail-order").show();
	}
	
	function hidePreviewOrder() {
		jQuery("#form-preview-order").hide();
	}
	
	function showPreviewOrder() {
		jQuery("#form-preview-order").show();
	}
	
	function showFormDate() {
		jQuery(".order-detail-button .buttonlist .form-date").show();
		jQuery(".order-detail-button .buttonlist .form-date").parent().addClass("show");
		jQuery(".order-detail-button .buttonlist .form-date").parent().find(".stdbtn").text("Xác nhận thời gian");
	}
	
	function hideFormDate() {
		jQuery(".order-detail-button .buttonlist .form-date").hide();
		jQuery(".order-detail-button .buttonlist .form-date").parent().removeClass("show");
		jQuery(".order-detail-button .buttonlist .form-date").parent().find(".stdbtn").text("Khách hẹn lại");
	}
	
	function cancelValue() {
		var selected = jQuery("#form-detail-order tbody tr.selected");
		var stt = selected.index();
		jQuery(selected).find(".change-number").each(function () {
			jQuery(this).find(".number").html("0");
			jQuery(this).find(".store-return").html(jQuery(this).find(".max").html());
		});
		
		//Update preview order
		var preview = jQuery("#form-preview-order tbody tr:eq("+stt+")");
		jQuery(preview).find(".change-number").each(function () {
			jQuery(this).find(".number").html("0");
			jQuery(this).find(".store-return").html(jQuery(this).find(".max").html());
		});
	}
	
	function getAllValue() {
		var value = "";
		jQuery("#form-detail-order tbody tr").each(function (i,e) {
			if (value != "") {
				value = value + ",";
			}
			var orderId = jQuery(this).find("td:eq(1)").html();
			value = value + orderId;
			jQuery(this).find(".change-number").each(function (i, e) {
				var productId = jQuery(this).find(".product-id").html();
				var storeReturn = jQuery(this).find(".store-return").html();
				if (i > 0) {
					value = value + "*";
				} else {
					value = value + "/";
				}
				value = value + productId + "-" + storeReturn;
			});
			
			var status = jQuery(this).find(".change-number:eq(0) .status").html();
			value = value + "/" + status;
			
			if (status == 3) {
				hideOrderDetail();
				showPreviewOrder();
				var dateReturn = jQuery(this).find(".change-number:eq(0) .date").html();
				value = value + "/" + dateReturn;
			}
			
		});
		return value;
	}
	
	function calcMoney() {
		var totalMoney = 0;
		jQuery("#list-order-preview tbody tr").each(function (i,e) {
			var totalNumber = 0;
			var money = parseInt(jQuery(this).find(".money").html());
			jQuery(this).find(".change-number").each(function (i, e) {
				var number = parseInt(jQuery(this).find(".number").html());
				totalNumber = totalNumber + number;
			});
			var total = totalNumber * money;
			
			jQuery(this).find(".totalMoney").html(formatMoney(total));
			totalMoney = totalMoney + total;
		});
		
		jQuery("#totalAllMoney").html(formatMoney(totalMoney));
	}
});

function formatMoney(n) {
    return (Math.round(n * 100) / 100).toLocaleString();
}





















/*jQuery(document).ready(function(){
	var input = jQuery('input[type=text]');
	var check = jQuery('#check');
	var listOrder = jQuery('#listOrder');
	check.click(function(){
		if(check.attr("id")=='check')
		{			
			var list = "";
			var valid = true;
			var totalMoney = 0;
			input.each(function(){
				var quantity = jQuery(this).parents("tr").find("td:eq(5)").text();
				var price = jQuery(this).parents("tr").find("td:eq(7)").text();
				var delivered = jQuery(this).val();
				if(quantity-delivered>=0)
				{
					price = price.replace(".","").replace(",","").replace("đ",""); 				
					var money = delivered*price;
					totalMoney+=money;
					jQuery(this).parents("tr").find("td:eq(8)").text(formatMoney(money)+"đ");
				}
				else
				{
					valid = false;
				}
				if(jQuery(this).val()!="") {
					list += jQuery(this).attr("name")+'-'+jQuery(this).val()+',';
				}
			});	
			listOrder.val(list);
			jQuery('#totalMoney').text(formatMoney(totalMoney)+"đ");
			if(valid)
			{
				check.attr("id","submit");	
				check.text("Submit");
			}
			else
			{
				alert("Giao nhận hàng không đúng !")
			}
			return false;
		}
	});
});

function formatMoney(n) {
    return (Math.round(n * 100) / 100).toLocaleString();
}*/